platform :ios, '15.0'

# use_frameworks!

# --------------- 本地公共 Pods ---------------
def common_pods
  pod 'BCComConfigKit', :subspecs => ['Core'], :path => '../SteelRecover/DevPods/BCComConfigKit'
  pod 'BCFoundation', :subspecs => ['Core', 'AttributedString', 'Device', 'Utils', 'Timer'], :path => '../SteelRecover/DevPods/BCFoundation'
  pod 'BCEventBus', :subspecs => ['Core', 'ServiceLoader'], :path => '../SteelRecover/DevPods/BCEventBus'
  pod 'BCDebugKit', :subspecs => ['Pannel', 'User', 'Device', 'File', 'Apm'], :path => '../SteelRecover/DevPods/BCDebugKit'
  pod 'BCFileLog', :subspecs => ['Core'], :path => '../SteelRecover/DevPods/BCFileLog'
  pod 'BCModel', :subspecs => ['Core'], :path => '../SteelRecover/DevPods/BCModel'
  pod 'BCRouteKit', :subspecs => ['Core'], :path => '../SteelRecover/DevPods/BCRouteKit'
  # pod 'BCScrollViewKit', :subspecs => ['Refresh', 'Empty', 'Business'], :path => '../SteelRecover/DevPods/BCScrollViewKit'
  pod 'BCUIKit', :subspecs => ['Alert', 'Core', 'Input', 'ScrollView', 'Label', 'NoSController'], :path => '../SteelRecover/DevPods/BCUIKit'
  pod 'BCWebImageKit', :subspecs => ['CoreNo', 'UINo'], :path => '../SteelRecover/DevPods/BCWebImageKit'
  pod 'HHCategoryView', :subspecs => ['Category', 'Pager'], :path => '../SteelRecover/DevPods/HHCategoryView'
  pod 'HHCollectionViewLayout', :subspecs => ['FlowLayout', 'Horizontal', 'Waterfall'], :path => '../SteelRecover/DevPods/HHCollectionViewLayout'
  pod 'HHFormSDK', :subspecs => ['Core'], :path => '../SteelRecover/DevPods/HHFormSDK'
  pod 'HHNetworkKit', :subspecs => ['Engine', 'Http', 'Download'], :path => '../SteelRecover/DevPods/HHNetworkKit'
  pod 'HHToastKit', :path => '../SteelRecover/DevPods/HHToastKit'
  pod 'HHToolsKit', :subspecs => ['Core'], :path => '../SteelRecover/DevPods/HHToolsKit'
  pod 'HHUIKit', :subspecs => ['Core', 'TagList', 'Transitioning', 'TableViewCell', 'Alert', 'BadgeView', 'CycleScrollView', 'Picker'], :path => '../SteelRecover/DevPods/HHUIKit'
  pod 'ZHPhotoBrowser', :path => '../SteelRecover/DevPods/ZHPhotoBrowser'
  pod 'BCAvoidCrash', :path => '../SteelRecover/DevPods/BCAvoidCrash'
  # pod 'BCLocationManager', :path => '../SteelRecover/DevPods/BCLocationManager'
  # pod 'AlipaySDK-iOS'
end

# --------------- 主 Target ---------------
target 'SteelRecover' do
  # 引入公共 Pods
  common_pods

  # 第三方远程 Pods
  pod 'QMUIKit', '4.7.0'
  pod 'IGListKit'
  pod 'Masonry'
  pod 'CYLTabBarController', '1.29.2'
  pod 'IQKeyboardManager'
  pod 'TZImagePickerController'
  pod 'DateTools'
  pod 'SDWebImageSVGCoder'
  pod 'SVProgressHUD', '~> 2.2.5'
  pod 'MJExtension'
  pod 'AFNetworking'
  pod 'MJRefresh'


  target 'SteelRecoverTests' do
    inherit! :search_paths
  end
end

# --------------- 后处理：关闭脚本沙箱 + 兼容 realpath + 修改写入目录 ---------------
post_install do |installer|
  # A) 关闭所有生成工程（Pods 工程等）里的 User Script Sandboxing
  installer.generated_projects.each do |project|
    project.targets.each do |t|
      t.build_configurations.each do |config|
        config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
      end
    end
    project.save
  end

  # B) 关闭用户工程（你的 App 工程）里所有 Target 的 User Script Sandboxing
  installer.aggregate_targets.each do |agg|
    user_project = agg.user_project
    user_project.targets.each do |t|
      t.build_configurations.each do |config|
        config.build_settings['ENABLE_USER_SCRIPT_SANDBOXING'] = 'NO'
      end
    end
    user_project.save
  end

  # C) 补丁：修复脚本里可能存在的 `realpath -m` 与写入路径不在临时目录的问题
  #    - 将 realpath -m 替换为跨平台的 python3 realpath
  #    - 将 $PODS_ROOT/resources-to-copy-* 改为 $TARGET_TEMP_DIR/resources-to-copy-*
  Dir.glob('Pods/Target Support Files/*/*-resources.sh').each do |script|
    begin
      content = File.read(script)

      patched = content.dup

      # 1) realpath -m 兼容（仅当出现时替换）
      if patched.include?('realpath -m')
        patched.gsub!(
          'realpath -m',
          "python3 -c 'import os,sys; print(os.path.realpath(sys.argv[1]))'"
        )
      end

      # 2) 将临时文件写入编译临时目录，避免触发沙箱（幂等替换）
      patched.gsub!(
        '$PODS_ROOT/resources-to-copy-',
        '$TARGET_TEMP_DIR/resources-to-copy-'
      )

      if patched != content
        File.open(script, 'w') { |f| f.write(patched) }
        puts "Patched: #{script}"
      end
    rescue => e
      puts "Skip patch #{script}: #{e}"
    end
  end
end

# --------------- 可选项：统一最低系统版本（按需开启）---------------
# post_install do |installer|
#   installer.pods_project.targets.each do |t|
#     t.build_configurations.each do |config|
#       config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.0'
#     end
#   end
# end
